package com.chinaso.kotlinprojcet

import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.chinaso.kotlinprojcet.bean.CSNews
import com.chinaso.kotlinprojcet.bean.GankNews
import kotlinx.android.synthetic.main.list_item.view.*

/**
 * Created by Administrator on 17-11-24.
 * 先写的java，然后使用快捷键alt+shift+ctrl+K转到kotlin文件的。
 */

class NewsAdapter(val list: List<CSNews>, val itemClickListener: (CSNews)->Unit) : RecyclerView.Adapter<NewsAdapter.NewsHolder>() {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): NewsHolder {
        val contentView: View
        contentView = LayoutInflater.from(parent.context).inflate(R.layout.list_item, parent, false)
        return NewsHolder(contentView, itemClickListener)
    }

    override fun onBindViewHolder(holder: NewsHolder, position: Int) {
        holder.bind(list[position])
    }

    override fun getItemCount(): Int {
        return list.size
    }

    inner class NewsHolder(itemView: View,val itemClickListener: (CSNews) -> Unit) : RecyclerView.ViewHolder(itemView) {
        fun bind(news: CSNews) {
            itemView.contentTV.text = news.title
            itemView.setOnClickListener {
                itemClickListener(news)
            }
        }
    }
}
